package com.linln.modules.roadrescue.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linln.common.utils.ObjectUtil;
import com.linln.common.vo.JsonResult;
import com.linln.common.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linln.modules.roadrescue.domain.RoadRescue;
import com.linln.modules.roadrescue.mapper.RoadRescueMapper;
import com.linln.modules.roadrescue.service.RoadRescueService;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.modules.roadrescue.service.impl
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.modules.roadrescue.service.impl
 * @Description: TODO
 * @date Date : 2021年01月07日 10:24
 */

@Service
public class RoadRescueServiceImpl extends ServiceImpl<RoadRescueMapper, RoadRescue> implements RoadRescueService {

    @Autowired
    private RoadRescueMapper roadRescueMapper;

    @Override
    public PageResult<RoadRescue> getRoadRescue(Integer pageNum, Integer limit, String search, Integer status) {
        PageResult<RoadRescue> pageResult = new PageResult<>();
        PageHelper.startPage(pageNum, limit);
        PageInfo<RoadRescue> pageInfo = new PageInfo<>(roadRescueMapper.selectRoadRescue(search, status,null));
        pageResult.setCount(pageInfo.getTotal());
        pageResult.setData(pageInfo.getList());
        pageResult.setCode(0);
        return pageResult;
    }

    @Override
    public boolean addRoadRescue(RoadRescue roadRescue) {
        return this.save(roadRescue);
    }


    @Override
    public boolean updateRoadRescueStatus(String rrId, Integer status, String statusNotes, String handlerDate) {
        return roadRescueMapper.updateRoadRescueStatus(rrId, status, statusNotes, handlerDate);
    }

    @Override
    public JsonResult getRoadRescueList(Integer page, Integer limit,Long userId) throws IllegalAccessException, JSONException, InvocationTargetException {
        PageHelper.startPage(page,limit);
        List<RoadRescue> roadRescueList = roadRescueMapper.selectRoadRescue(null,null,userId);
        PageInfo<RoadRescue> pageInfo = new PageInfo<>(roadRescueList);

        for(RoadRescue roadRescue : roadRescueList){
            roadRescue.setTextNew("发起时间：");
            roadRescue.setTimeNew(roadRescue.getAlarmDate());
            Map<String,Object> props = roadRescue.getProps();
            if(props!=null){
                if(props.containsKey("processing")){
                    Map m = (Map) props.get("processing");
                    roadRescue.setTextNew("处理时间：");
                    roadRescue.setTimeNew(m.get("handlerDate").toString());
                }
                if(props.containsKey("processed")){
                    Map m = (Map) props.get("processed");
                    roadRescue.setTextNew("完成时间：");
                    roadRescue.setTimeNew(m.get("handlerDate").toString());
                }
            }
        }

        JsonResult jsonResult = JsonResult.ok();
        jsonResult.put("code",200);
        jsonResult.put("data",pageInfo.getList());
        jsonResult.put("total",pageInfo.getTotal());
        return jsonResult;
    }
}
